<?php

  if (isset($_POST['search_string'])) {
    Session::Set('fst', $_POST['search_string']);
    Session::Set('sst', "%{$_POST['search_string']}%"); // Suchbegriff setzen
    Session::Set('ssc', base::get('controller'));       // Controller setzen
    if ($_POST['methode'] != 'search') {
      Session::Set('ssm', $_POST['methode']);
    }
    $s = True;
  }

  $db = new Database('SMT-ADMIN');
  $query = "SELECT *, DATE_FORMAT(datum,'%d.%m.%Y %H:%i') AS niceDatum FROM wos_ticket WHERE "
          . "id LIKE :sst && tid=:tid || "
          . "titel LIKE :sst && tid=:tid || "
          . "beschreibung LIKE :sst && tid=:tid ORDER BY id DESC";
  $value = array(':sst' => Session::get('sst'), ':tid' => 0);

  $db->getQuery($query, $value);
    
  if ($db->getNumrows() >= 1 && isset($s)) {
    $new_url = $_SERVER['PHP_SELF'] . '/' . $_SERVER['QUERY_STRING'];
    header('Location:' . str_replace("index.php/", "", $new_url));
  }

  $list = $db->getValue();
  
  for($l=0; $l<count($list); $l++) {
    $detail = $ticket->readTicket($list[$l]['id']);
    include('ticket_details.php');;
    
    $tmp[$l] = $detail;
    unset($detail);
  }
  
  if(isset($tmp)) {
    if(count($tmp) == 1) {
      header("Location: " . base::get('getPath') . "/ticket/detail/".$tmp['0']['ticket']['0']['id']);
    } else {
      template::setText('liste', $tmp);
    }
  }
  
?>

